package com.tw.community.helpers
{
	//
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;
    import flash.net.URLRequest;
    import flash.utils.*;

    public class AudioManager extends Sprite
    {
    	//props
    	[Bindable]
    	public static var soundFactory:Sound;
    	[Bindable]
		public static var song:SoundChannel;
		//boolean that keeps track of whether or not the current track is playing
		public static var isPlaying:Boolean;
		//boolean that indicates if the track is currently paused
		public static var isPaused:Boolean;
		//boolean that indicates if the track is done
		public static var donePlaying:Boolean;
		//current position of the track in milliseconds
		public static var curPos:Number;
    	//constructor
    	public function AudioManager(){
    		
    		
    	}
    	/*
    	 *	Method that loads up the song for play back
    	 *	@param	u	the url/path to mp3
    	 */
    	public static function loadSong(u:String):void{
			//checking to see if song has ever been instiated
			if(song != null){
				song.removeEventListener(Event.SOUND_COMPLETE, songDone);
			}
			//initializing props
			donePlaying = false;
			curPos = 0;
			//checking to see if there may be a track already playing
			if(isPlaying == true){
				unLoadSong();
			}
			//setting up our request for the new track
			var request:URLRequest = new URLRequest(u);
			//creating our sound object
			soundFactory = new Sound();
			//actually loading the track
			soundFactory.load(request);
			
			
			
			
		
		}
		
		/*
		 * Event handler that is invoked when the song is done playing
		 */
		private static function songDone(evt:Event):void{
			
			donePlaying = true;
		}
		/*
		 *	Method to start playing song
		 */
		public static function playSong():void{
			
			song = soundFactory.play(curPos);	
			song.addEventListener(Event.SOUND_COMPLETE, songDone);
			isPlaying = true;
			isPaused = false;
			
		}
		/*
		 *	Method used to skip to a position of the track
		 *	@param	n	the pos in milliseconds
		 */
		public static function skipToPos(n:Number):void{
			pauseSong();
			curPos = n;
			playSong();
			
		}
		/*
		*	Method to set the audio volume
		*	@param	n	0-100 the level you want
		*/
		public static function setVolume(n:Number):void{
			var st:SoundTransform = new SoundTransform();
			st.volume = n/100;
			song.soundTransform = st;
			
		}
		/*
		 *	Method that returns the current volume of the sound playing
		 */
		public static function getVolume():Number{
			return song.soundTransform.volume * 100;
		}
		/*
		 *	Method to pause the song/track
		 */
		public static function pauseSong():void{
			isPaused = true;	
			isPlaying = false;
			curPos = song.position;
			song.stop();
		}
		/*
		 *	Method used to get the length of the current track
		 */
		public static function getSongLength():Number{
			return soundFactory.length;
		}
		/*
		 *	Method used to get the current position of the track
		 */
		public static function getSongPos():Number{
			return song.position;
		}
		/*
		 *	Method used to clear the current track playing
		 */
		public static function unLoadSong():void{
			if(soundFactory != null){
				if(isPlaying == true){
					song.stop();
					isPlaying = false;
				}
				if(soundFactory.isBuffering == true){
					soundFactory.close();	
				}
			}
		}
    }

}